package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.core.toolbox.kit.StrKit;
import com.sixmac.model.Banner;
import com.sixmac.model.User;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/6/9. 发现
 */
@Controller
@ResponseBody
public class FindController extends ApiController {
    /**
     * @api {post} /api/banner  benner
     * @apiName find.banner
     * @apiGroup find
     * @apiSuccess {String}  article  要跳转的文章id
     * @apiSuccess {String}  content  概述内容
     * @apiSuccess {String}  img  封面
     * @apiError {Integer} 1
     */
    @RequestMapping("/banner")
    public AjaxResult banner() {
        AjaxResult res = new AjaxResult();
        List<Banner> lists = Blade.create(Banner.class).findAll();
        res.setData(CMap.init().put("banner", lists));
        return res;
    }

    /**
     * @api {post} /api/find  发现
     * @apiName find.find
     * @apiGroup find
     * @apiParam {String}  token  token
     * @apiParam {String}  page  当前页 默认为1
     * @apiParam {String}  size  每页大小 默认为3
     * @apiSuccess {String} data.rows.id 文章id
     * @apiSuccess {String} data.rows.img 封面图片
     * @apiSuccess {String} data.rows.title 标题
     * @apiSuccess {String} data.rows.length 视频播放时间
     * @apiSuccess {String} data.rows.url 地址
     * @apiSuccess {String} data.rows.content 内容
     * @apiSuccess {String} data.rows.palynum 播放量
     * @apiSuccess {String} data.rows.thumbsnum 点赞量
     * @apiSuccess {String} data.rows.isthumb 自己是否点赞 0 没 1是
     * @apiSuccess {String} data.rows.iscollection 自己是否收藏 0 没 1是
     * @apiError {Integer} 1
     */
    @AuthUser(requisite = false)
    @RequestMapping("/find")
    public AjaxResult find(User user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        AjaxResult res = new AjaxResult();
        BladePage<Map> list = null;
        if (user == null || user.getId() == null) {
            list = Db.paginate("select *,0 as 'isCollection' from t_content c", null, page, size);
        } else {
            list = Db.paginate("select *,(select count(id) from t_collections where userId = " + user.getId() + " and articleId= c.id) as 'isCollection' from t_content c", null, page, size);
        }
        return res.setData(list);
    }

    /**
     * @api {post} /api/findCategroy  发现分类
     * @apiName find.findCategroy
     * @apiGroup find
     * @apiSuccess {String} categories.id 分类id
     * @apiSuccess {String} categories.name 分类名称
     * @apiError {Integer} 1
     */
    @RequestMapping("/findCategroy")
    public AjaxResult findCategroy() {
        AjaxResult res = new AjaxResult();
        List list = Db.selectList("select * from t_category where 1=1 and pid!=0 ");
        res.setData(CMap.init().put("categories", list));
        return res;
    }

    /**
     * @api {post} /api/findArticleByCategoryId  通过分类id查找文章
     * @apiName find.findArticleByCategoryId
     * @apiGroup find
     * @apiParam {String}  categoryId  分类id
     * @apiParam {String}  page  当前页 默认为1
     * @apiParam {String}  size  每页大小 默认为3
     * @apiSuccess {String} data.rows.id 文章id
     * @apiSuccess {String} data.rows.img 封面图片
     * @apiSuccess {String} data.rows.title 标题
     * @apiSuccess {String} data.rows.length 视频播放时间
     * @apiSuccess {String} data.rows.url 地址
     * @apiSuccess {String} data.rows.content 内容
     * @apiSuccess {String} data.rows.palynum 播放量
     * @apiSuccess {String} data.rows.thumbsnum 点赞量
     * @apiSuccess {String} data.rows.isthumb 自己是否点赞 0 没 1是
     * @apiSuccess {String} data.rows.iscollection 自己是否收藏 0 没 1是
     * @apiError {Integer} 1
     */
    @RequestMapping("/findArticleByCategoryId")
    public AjaxResult findArticleByCategoryId(Integer categoryId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        AjaxResult res = new AjaxResult();
        BladePage<Map> maps = Db.paginate("select * from t_content where categoryId =" + categoryId, null, page, size);
        return res.setData(maps);


    }


    /**
     * @api {post} /api/search  搜索视频
     * @apiName find.search
     * @apiGroup find
     * @apiParam {String}  token  token
     * @apiParam {String}  title  标题
     * @apiParam {Integer}  day  1天内2一周内3一年内  不传表示全部
     * @apiParam {Integer}  order  1观看2点赞3评论 不传表示全部
     * @apiParam {Integer}  categoryId  展览 到视频 1到8 全部不传
     * @apiParam {String}  size  每页大小 默认为3
     * @apiParam {String}  page  当前页 默认为1
     * @apiParam {String}  size  每页大小 默认为3
     * @apiSuccess {String} data.rows.id 文章id
     * @apiSuccess {String} data.rows.img 封面图片
     * @apiSuccess {String} data.rows.title 标题
     * @apiSuccess {String} data.rows.length 视频播放时间
     * @apiSuccess {String} data.rows.url 地址
     * @apiSuccess {String} data.rows.content 内容
     * @apiSuccess {String} data.rows.palynum 播放量
     * @apiSuccess {String} data.rows.thumbsnum 点赞量
     * @apiSuccess {String} data.rows.iscollection 自己是否收藏 0 没 1是
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/search")
    public AjaxResult search(User user, String title, Integer day, Integer order, Integer categoryId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size) {
        AjaxResult res = new AjaxResult();
        StringBuilder sb = new StringBuilder();
        sb.append("select *,(select count(id) from t_collections where userId = " + user.getId() + " and articleId= t_content.id) as 'isCollection' from t_content where 1= 1 ");
        if (StrKit.isNotEmpty(title)) {
            sb.append(" and title like '%" + title + "%'");
        }
        if (day != null) {
            switch (day) {
                case 1:
                    sb.append(" and to_days(createDate)=to_days(now()) ");
                    break;
                case 2:
                    sb.append(" and createDate between current_date()-7 and sysdate() ");
                    break;
                case 3:
                    sb.append(" and createDate>DATE_SUB(CURDATE(), INTERVAL 1 YEAR) ");
                default:
                    break;
            }
        }
        if (categoryId != null) {
            String sql1 = "select id from t_category where pid = " + (categoryId + 1);
            List<Map> lists = Db.selectList(sql1, null);
            StringBuilder s = new StringBuilder();
            for (Map m : lists) {
                s.append(m.get("id"));
                s.append(",");
            }
            String sql2 = s.substring(0, s.length() - 1);
            sql2 = " and categoryId in (" + sql2 + ")";
            sb.append(sql2);

        }

        if (order != null) {
            switch (order) {
                case 1:
                    sb.append(" order by palynum desc ");
                    break;
                case 2:
                    sb.append(" order by thumbsnum desc");
                    break;
                case 3:
                    sb.append(" order by commentnum desc");
                    break;
            }
        }

        BladePage<Map> list = Db.paginate(sb.toString(), null, page, size);
        return res.setData(list);
    }


    @Test
    public void te() {
        int n = 2;
        switch (n) {
            case 1:
                System.out.print(1);
            case 2:
                System.out.print(2);
            case 3:
                System.out.print(3);
            case 4:
                System.out.print(4);
            default:
                System.out.print(5);
        }


    }


}
